package cn.zhagzheguo.xk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.zhagzheguo.xk.dao.CourseSelectDao;
import cn.zhagzheguo.xk.model.Course;
import cn.zhagzheguo.xk.model.CourseSelect;

@Controller
@RequestMapping("/hello")
public class HelloController {

	@Autowired
	CourseSelectDao csDao;

	@RequestMapping("/test")
	public String test() {
		System.out.println("test request.");
		return "first.jsp";
	}

	@RequestMapping("/test1")
	public String test1() {
		System.out.println("test1 request.");
		return "list.html";
	}

	@RequestMapping("/test2")
	public ModelAndView test2() {
		System.out.println("test2 request.");
		return new ModelAndView("list.html");
	}

	@RequestMapping("/test3")
	public ModelAndView test3() {
		System.out.println("test3 request.");
		return new ModelAndView("index.jsp");
	}

	@RequestMapping("/testParam")
	public ModelAndView testParam() {
		System.out.println("testParam request.");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("test", "success1");
		return new ModelAndView("list.html", map);
	}

	@RequestMapping("/testModel")
	public String testModel(Model model) {
		System.out.println("testModel request.");
		model.addAttribute("test", "success2");
		return "first";
	}

	@RequestMapping("/login")
	public String login(@RequestParam("stuid") String stuid, @RequestParam("pwd") String pwd) {
		System.out.println("stuid=" + stuid + ",pwd=" + pwd);
		Map<String, Object> map = new HashMap<>();
		map.put("loginMsg", "success3");
		return "index";
	}

	@RequestMapping("/testTrans")
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 20, rollbackFor = { ArithmeticException.class }, value = "txManager")
	public void testTrans() {
		System.out.println("in testTrans");
		CourseSelect cs = new CourseSelect();
		cs.setCourse_id(4);
		cs.setDate("啊啊啊");
		csDao.insert(cs);
		int i = 1 / 0;
		List<Course> css = csDao.getBriefByStuId(10);
	}

	public void setCsDao(CourseSelectDao csDao) {
		this.csDao = csDao;
	}

}
